package cn.gok.manage.service.impl;

import cn.gok.dao.ShippingMapper;
import cn.gok.manage.service.IShippingService;
import cn.gok.manage.web.vo.ShippingVO;
import cn.gok.pojo.Shipping;
import cn.gok.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author: 鸭鸭
 * @ClassName:ShippingServiceImpl
 * @Package:cn.gok.manage.service.impl
 * @Description:
 * @date: 2021/8/4
 * @time: 16:33
 */
@Service("ShippingService")
public class ShippingServiceImpl implements IShippingService {
    @Autowired
    ShippingMapper shippingMapper;
    @Override
    public ResultData insertShipping(Shipping record) {
        int id = shippingMapper.insertSelective(record);
        if(id == 0){
            return ResultData.fail("新建地址失败");
        }
        return new ResultData("0",record.getId(),"新建地址成功");
    }

    @Override
    public ResultData deleteShippingById(int id) {
        if(shippingMapper.deleteByPrimaryKey(id) == 0){
            return ResultData.fail("删除地址失败");
        }
        return ResultData.success("删除地址成功");
    }
    @Override
    public ResultData updateShippingInLogin(Shipping record) {
        Date date = new Date();
        record.setUpdateTime(date);
        int flag = shippingMapper.updateByPrimaryKeySelective(record);
        if(flag==0){
            return ResultData.fail("更新地址失败");
        }
        return ResultData.success("更新地址失败成功");
    }

    @Override
    public ResultData selectShippingById(int id) {
        Shipping record = shippingMapper.selectByPrimaryKey(id);
        if(record == null){
            return ResultData.fail("请登录之后查询");
        }
        return ResultData.createData(record);
    }

    @Override
    public ResultData selectAllShipping(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Shipping> records = shippingMapper.selectAllShipping();
        if(records == null){
            return ResultData.fail("请登录之后查询");
        }
        //获得分页信息
        PageInfo<Shipping> pageInfo = new PageInfo<Shipping>(records);
        ShippingVO shippingVO = new ShippingVO();
        shippingVO.setPageNum(pageNum);
        shippingVO.setPageSize(pageSize);
        shippingVO.setSize(pageInfo.getSize());
        shippingVO.setOrderBy(null);
        shippingVO.setStartRow(pageInfo.getStartRow());
        shippingVO.setEndRow(pageInfo.getEndRow());
        shippingVO.setTotal(pageInfo.getTotal());
        shippingVO.setPages(pageInfo.getPages());
        shippingVO.setRecords(pageInfo.getList());
        shippingVO.setFirstPage(pageInfo.getNavigateFirstPage());
        shippingVO.setPrePage(pageInfo.getPrePage());
        shippingVO.setNextPage(pageInfo.getNextPage());
        shippingVO.setLastPage(pageInfo.getNavigateLastPage());
        if(pageInfo.getPageNum()==1){
            shippingVO.setFirstPage(true);
            shippingVO.setHasPreviousPage(false);
        }else{
            shippingVO.setFirstPage(false);
            shippingVO.setHasPreviousPage(true);
        }
        if(pageInfo.getNavigateLastPage()==pageInfo.getPageNum()) {
            shippingVO.setLastPage(true);
            shippingVO.setHasNextPage(false);
        }else{
            shippingVO.setLastPage(false);
            shippingVO.setHasNextPage(true);
        }
        shippingVO.setNavigatePages(pageInfo.getNavigatePages());
        shippingVO.setNavigatepageNums(pageInfo.getNavigatepageNums());

        return ResultData.createData(shippingVO);


    }
}
